package com.zeus.web.utils;

/**
 * @author follow
 * @ClassName IpLongUtils
 * @Description ip转换工具类
 * @Date 2018/4/18 15:48
 */
public class IpLongUtils {
    /**
     *@Description 把字符串IP转换成long
     *@author follow
     *@Date 2018/4/18 15:49
     *@Param
     *@return
     **/
    public static long ip2Long(String ipStr) {
        String[] ip = ipStr.split("\\.");
            return (Long.valueOf(ip[0]) << 24) + (Long.valueOf(ip[1]) << 16)
                + (Long.valueOf(ip[2]) << 8) + Long.valueOf(ip[3]);
        }
        /**
         *@Description 把IP的long值转换成字符串
         *@author follow
         *@Date 2018/4/18 15:50
         *@Param
         *@return
         **/
    public static String long2Ip(long ipLong) {
        StringBuilder ip = new StringBuilder();
        ip.append(ipLong >>> 24).append(".");
        ip.append((ipLong >>> 16) & 0xFF).append(".");
        ip.append((ipLong >>> 8) & 0xFF).append(".");
        ip.append(ipLong & 0xFF);
        return ip.toString();
    }

    public static void main(String[] args) {
        System.out.println(ip2Long("192.168.0.1"));
        System.out.println(long2Ip(3232235521L));
        System.out.println(ip2Long("10.0.0.1"));
    }

}
